//
//  NSObject+Unicode.m
//
//  Created by KingTouch on 15/12/7.
//  Copyright © 2015年 KingTouch. All rights reserved.
//

#import "NSObject+Unicode.h"
#import <objc/runtime.h>

@implementation NSObject (Unicode)

+(NSString *)stringByReplaceUnicodeString:(NSString *)unicodeString
{
    NSMutableString *converedString = [unicodeString mutableCopy];
    [converedString replaceOccurrencesOfString:@"\\U" withString:@"\\u" options:NSCaseInsensitiveSearch range:NSMakeRange(0, converedString.length)];
    CFStringRef transform = CFSTR("Any-Hex/Java");
    CFStringTransform((__bridge CFMutableStringRef)converedString, NULL, transform, YES);
    return converedString ;
}

@end

@implementation NSArray (Unicode)

+(void)load
{
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(description)), class_getInstanceMethod([self class], @selector(replaceDescription)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:indent:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:indent:)));
}
-(NSString *)replaceDescription
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescription]];
}
-(NSString *)replaceDescriptionWithLocale:(NSString *)string
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescriptionWithLocale:string]];
}
-(NSString *)replaceDescriptionWithLocale:(NSString *)string indent:(NSUInteger)level
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescriptionWithLocale:string indent:level]];
}

@end

@implementation NSDictionary (Unicode)

+ (void)load
{
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(description)), class_getInstanceMethod([self class], @selector(replaceDescription)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:indent:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:indent:)));
}

- (NSString *)replaceDescription
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescription]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescriptionWithLocale:locale]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale indent:(NSUInteger)level
{
    return [NSObject stringByReplaceUnicodeString:[self replaceDescriptionWithLocale:locale indent:level]];
}

@end
